package cn.stylefeng.guns.modular.business.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.modular.business.entity.SubDic;
import cn.stylefeng.guns.modular.business.enums.SubDicExceptionEnum;
import cn.stylefeng.guns.modular.business.mapper.SubDicMapper;
import cn.stylefeng.guns.modular.business.pojo.SubDicRequest;
import cn.stylefeng.guns.modular.business.service.SubDicService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 检索词典业务实现层
 *
 * @author zhangyaowen
 * @date 2021/08/30 10:17
 */
@Service
public class SubDicServiceImpl extends ServiceImpl<SubDicMapper, SubDic> implements SubDicService {

    @Override
    public void add(SubDicRequest subDicRequest) {
        SubDic subDic = new SubDic();
        BeanUtil.copyProperties(subDicRequest, subDic);
        this.save(subDic);
    }

    @Override
    public void del(SubDicRequest subDicRequest) {
        SubDic subDic = this.querySubDic(subDicRequest);
        this.removeById(subDic.getId());
    }

    @Override
    public void edit(SubDicRequest subDicRequest) {
        SubDic subDic = this.querySubDic(subDicRequest);
        BeanUtil.copyProperties(subDicRequest, subDic);
        this.updateById(subDic);
    }

    @Override
    public SubDic detail(SubDicRequest subDicRequest) {
        return this.querySubDic(subDicRequest);
    }

    @Override
    public PageResult<SubDic> findPage(SubDicRequest subDicRequest) {
        LambdaQueryWrapper<SubDic> wrapper = createWrapper(subDicRequest);
        Page<SubDic> sysRolePage = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(sysRolePage);
    }

    @Override
    public List<SubDic> findList(SubDicRequest subDicRequestList) {
        LambdaQueryWrapper<SubDic> wrapper = this.createWrapper(subDicRequestList);
        return this.list(wrapper);
    }
    @Override
    public List<SubDic> findAllList(List<SubDicRequest> subDicRequestList) {
        LambdaQueryWrapper<SubDic> wrapper = this.createWrappers(subDicRequestList);
        return this.list(wrapper);
    }
    /**
     * 获取信息
     *
     * @author zhangyaowen
     * @date 2021/08/30 10:17
     */
    private SubDic querySubDic(SubDicRequest subDicRequest) {
        SubDic subDic = this.getById(subDicRequest.getId());
        if (ObjectUtil.isEmpty(subDic)) {
            throw new ServiceException(SubDicExceptionEnum.SUBDIC_NOT_EXISTED);
        }
        return subDic;
    }

    /**
     * 创建查询wrapper
     *
     * @author zhangyaowen
     * @date 2021/08/30 10:17
     */
    private LambdaQueryWrapper<SubDic> createWrapper(SubDicRequest subDicRequest) {
        LambdaQueryWrapper<SubDic> queryWrapper = new LambdaQueryWrapper<>();

        Long id = subDicRequest.getId();
        String dicAname = subDicRequest.getDicAname();
        String dicZname = subDicRequest.getDicZname();
        Long dicType = subDicRequest.getDicType();

        queryWrapper.eq(ObjectUtil.isNotNull(id), SubDic::getId, id);
        queryWrapper.like(ObjectUtil.isNotEmpty(dicAname), SubDic::getDicAname, dicAname);
        queryWrapper.like(ObjectUtil.isNotEmpty(dicZname), SubDic::getDicZname, dicZname);
        queryWrapper.eq(ObjectUtil.isNotNull(dicType), SubDic::getDicType, dicType);

        return queryWrapper;
    }

    /**
     * 创建查询wrapper
     *
     * @author zhangyaowen
     * @date 2021/08/30 10:17
     */
    private LambdaQueryWrapper<SubDic> createWrappers(List<SubDicRequest> subDicRequestList) {
        LambdaQueryWrapper<SubDic> queryWrapper = new LambdaQueryWrapper<>();
        Long dicType = subDicRequestList.get(0).getDicType();
        queryWrapper.eq(ObjectUtil.isNotNull(dicType), SubDic::getDicType, dicType);
        for(int i=0 ;i<subDicRequestList.size();i++) {

            String dicZname = subDicRequestList.get(i).getDicZname();
            queryWrapper.like(ObjectUtil.isNotEmpty(dicZname), SubDic::getDicZname, dicZname)
                    .or()
                    .like(ObjectUtil.isNotEmpty(dicZname), SubDic::getDicAname, dicZname);
            if(i !=subDicRequestList.size()-1) {
                queryWrapper.or();
            }
        }
        return queryWrapper;
    }

}